﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Driver;
using ValuationResource;
using ValuationLogic;
namespace ValuationModel
{

    public class UserModel
    {
        private MongoServer server;
        private MongoDatabase mydb;
        private MongoCollection coll;
        private MongoCollection collUser;
        private MongoCollection collSettlement;
        private MongoCollection collContract;
        string connectionString = "mongodb://localhost";
        string databaseName = "mydb";
  
        //声明一个MongoCollection对象，调用它的insert方法来插入数据
        

        //private Mongo mongo;
        //private MongoDatabase mongoDatabase;
        //private MongoCollection<Document> mongoCollection;

        public UserModel()
        {
            //mongo = new Mongo(connectionString);
            //mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
            //mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
            //mongo.Connect();
         
            server = MongoServer.Create(connectionString);
            //得到一个数据库实例的连接
            MongoCredentials credentials = new MongoCredentials("","");
            //便于以后设定安全证书
            mydb = server.GetDatabase(databaseName);
            //获得一个“mydb”的连接对象
            coll = mydb.GetCollection("mycl");//声明一个collection对象
            collUser = mydb.GetCollection("myclUser");
            collSettlement = mydb.GetCollection("myclSet");
            collContract = mydb.GetCollection("myclCon");
            server.Connect();
          
        }
        ~UserModel()
        {
            server.Disconnect();
        }

       
        /// 增加一条用户swap记录
        public void Add(BsonDocument doc)
        {
            coll.Insert(doc);
         }
        /// 增加一条user记录
        public void AddUser(BsonDocument doc)
        {
            coll.Insert(doc);
        }
        public bool isValidation(BsonDocument doc)
        {
            ///
            ///To be done
            ///
            return true;
        }
        /// <summary>
        /// 更新一条用户swap记录
        /// </summary>
        /// <param name="doc"></param>
        public void Update(BsonDocument doc)   
        {
           // coll = mydb.GetCollection(collectionName);
            var queryDoc = new QueryDocument { { "swapId", doc["swapId"] } };
            //只能针对PK修改，
            //通过构造函数QueryDocument定义一个查询对象，相当于SQL中的where语句
            BsonDocument newDoc = new BsonDocument 
            {
                {"risk", doc["risk"]},
                {"PnL", doc["PnL"]}
            };
            var updateDoc = new UpdateDocument { { "$set", newDoc } };
            //通过构造函数UpdateDocument定义一个更新对象，相当于SQL中的set语句
            coll.Update(queryDoc, updateDoc);
            //通过update方法完成更新
            Console.WriteLine("updateSwap !!");
            //coll.FindAndModify(queryDoc,doc);
           
         
        }

        
         //查找所有用户记录
        public IEnumerable<BsonDocument> FindAll()
        {
            return coll.FindAllAs<BsonDocument>();
            //return coll.FindAll().Documents;
        }
        /// 获取用户全部swap列表
        public void UserList(string userId)
        {
            IEnumerable<BsonDocument> list = coll.FindAllAs<BsonDocument>();
            //List<Swap> myList;
            int i = 0;
            var query = from u in list
                        select new
                        {
                            id = i++,
                            cell = new string[]{
                                u["UserId"].ToString(),
                                u["UserName"].ToString(),
                                u["Price"].ToString(),
                                u["Amount"].ToString(),
                                u["BorS"].ToString()
                            }
                        };
            

           
        }

        //public List<Swap> selectAll()
        //{
        //    List<Swap> list = new List<Swap>();
        //    list.AddRange();
        //    return list;
        //}
        public void selectAll(string userId)
        {
            coll = mydb.GetCollection("mycl");
            coll.EnsureIndex("userId");
            IEnumerable<BsonDocument> list = (IEnumerable<BsonDocument>)coll.GetIndexes();
            IEnumerator<BsonDocument> col = list.GetEnumerator();
            while (col.MoveNext())
            {
                Console.WriteLine(col.Current.ToString()+"\n");
            }
            Console.ReadLine();
        }
        
    }
}